package com.poe.controller.cart;

import com.poe.entity.Cart;
import com.poe.entity.User;
import com.poe.service.icart.CartService;
import com.poe.vo.CartReturn;
import com.util.Util;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {
	@Autowired
	private CartService cartService;
	@RequestMapping("lookCart")
	public String lookCart(){
		return "cart/cart";
	}
	
//添加给购物车商品
	@RequestMapping("addProduct")
	public void addProduct(HttpServletResponse response, HttpSession session, String bookName, String bookWriter, String bookId, int bookPrice, int bookNum) throws IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw=response.getWriter();
		Cart cart=new Cart();
		//要返回的结果
		CartReturn cartReturn=new CartReturn();
		User user1=(User) session.getAttribute("user1");
		if(user1==null){
			cartReturn.setCartString("未登录,请先登录!");
		}else{
			cart.setBookId(bookId);
			cart.setBookName(bookName);
			cart.setBookWriter(bookWriter);
			cart.setBookNum(bookNum);
			cart.setBookPrice(bookPrice);
			cart.setCartId(Util.getUUId());
			cart.setUserId(user1.getUserId());
			cartService.addProduct(cart);
			cartReturn.setCartString("加入购物车成功!");
		}
		String a= JSONObject.fromObject(cartReturn).toString();
    	pw.write(a);
    	pw.flush();
    	pw.close();
	}
//查询购物车所有的商品
	@RequestMapping("cart")
	public void cart(HttpServletResponse response, HttpSession session) throws IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw=response.getWriter();
		CartReturn cartReturn=new CartReturn();
		int cartCountPrice = 0;
		User user1=(User) session.getAttribute("user1");
		if(user1==null){
			cartReturn.setCartString("未登录,请先登录!");
		}else{
			List<Cart> list=cartService.findAllCart(user1.getUserId());
			if(list!=null){
				cartReturn.setList(list);
				cartReturn.setCartNum(list.size());
				for(Cart c:list){
					cartCountPrice+=c.getBookPrice()*c.getBookNum();
				}
				cartReturn.setCartCountPrice(cartCountPrice);
			}
		}
		String a=JSONObject.fromObject(cartReturn).toString();
    	pw.write(a);
    	pw.flush();
    	pw.close();
	}
	
//删除购物车不想要的商品	
	@RequestMapping("deleteProduct")
	public void deleteProduct(HttpServletResponse response, String cartId, HttpSession session) throws IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw=response.getWriter();
		CartReturn cartReturn=new CartReturn();
		int cartCountPrice = 0;
		boolean bl=cartService.deleteProduct(cartId);
		if(bl){
			User user1=(User)session.getAttribute("user1");List<Cart>
			list=cartService.findAllCart(user1.getUserId());
			if(list!=null){
				cartReturn.setCartString("删除成功!");
				cartReturn.setList(list);
				cartReturn.setCartNum(list.size());
				for(Cart c:list){
					cartCountPrice+=c.getBookPrice()*c.getBookNum();
				}
				cartReturn.setCartCountPrice(cartCountPrice);
			}
		}
		String a=JSONObject.fromObject(cartReturn).toString();
    	pw.write(a);
    	pw.flush();
    	pw.close();
	}
}
